• File: does_it_exist.php
  • Full Path: C:/htdocs/reeft_gps_test/REEFTintegrationLog/does_it_exist.php
  • Date Modified: 05/26/2025 3:20 PM
  • File size: 7.48 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
//======================================================================================
//
// Function: Check if a path or file exists
//
// Programmer: JKJ
// Date      : 2025-05-08
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================

//======================================================================================
// General config
//======================================================================================
	include "config/config.php";

//======================================================================================
// Set language
//======================================================================================

	include "include/set_language.php";

//======================================================================================
// Get input
//======================================================================================

	if (isset($_REQUEST["input_string"])) {
		$input_string = $_REQUEST["input_string"];
	} else {
		$input_string = '';
	}

	// $input_string = 'sqlite_admin_reeft/phpliteadmin_themes.zip';


//======================================================================================
// Set defaults
//======================================================================================

	$entries_found = 0;

	$currentDate = date('Y-m-d');
	$currentTime = date('H:i:s');

	$returnCode = '00';
	$returnMsg 	= 'Alles ist gut';

	$input_type		 	= '';

	$input_string_file 	= 'N';
	$input_string_dir 	= 'N';
	$file_bytes	   		= 0;

	$file_bytes 	 	= 0;
	$file_bytes_text 	= '*NONE';

	$dirname 			= '*NONE';
	$basename 			= '*NONE';
	$extension  		= '*NONE';
	$filename 			= '*NONE';

//======================================================================================
// Set header
//======================================================================================
	header('Content-Type: application/json;charset=utf-8');

//======================================================================================
// Start me up...
//======================================================================================
	$starttime = microtime(true);

//======================================================================================
// Check if database file exists
//======================================================================================
	if (!file_exists($input_string)) {
		$input_exists = 'N';
		$entries_found = 0;
		$returnCode = '99';
		$returnMsg 	= 'Object not found';
		
	} else {
		$input_exists = 'Y';
		$entries_found = 1;
	}

//======================================================================================
// Does it exsit
//======================================================================================
	if ( $input_exists == 'Y' )
	{

		//======================================================================================
		// Is it a file?
		//======================================================================================
		$myBool_file = is_file( $input_string );
		if ( $myBool_file === true ) {
			$input_string_file = 'Y';
			$input_type		   = '*FILE';
		} else {
			$input_string_file = 'N';
		}

		//======================================================================================
		// Is it a dir?
		//======================================================================================
		$myBool_dir = is_dir( $input_string );
		if ( $myBool_dir === true ) {
			$input_string_dir = 'Y';
			$input_type		  = '*DIR';
		} else {
			$input_string_dir = 'N';
		}

		//======================================================================================
		// Get path / file info
		//======================================================================================
		$path_parts = pathinfo( $input_string );

		@$dirname 	= $path_parts['dirname'];
		@$basename 	= $path_parts['basename'];
		@$extension = $path_parts['extension'];
		@$filename 	= $path_parts['filename'];

		//======================================================================================
		// Get bytes size - only if is a file
		//======================================================================================
		if ( $input_string_file == 'Y' ) {
			@$file_bytes = filesize($input_string);
			$file_bytes_text = fileSizeConvert( $file_bytes );
		}
		
	}

//======================================================================================
// Calculate response time
//======================================================================================
	$endtime 			= microtime(true);
	$response_time 		= $endtime - $starttime;
	$response_time 		= number_format($response_time, 6, '.', '');
	$response_time_raw 	= number_format($response_time, 6, '.', '');
	$response_time 		= '(' . $response_time . ' seconds)';
	$response_time_raw 	= $response_time_raw;

//======================================================================================
// Create header
//======================================================================================
	$aryHeader = array();

	$aryHeader["entries_found"] 				= $entries_found;
	$aryHeader["returnCode"] 					= $returnCode;
	$aryHeader["returnMsg"] 					= $returnMsg;

	$aryHeader["currentDate"] 					= $currentDate;
	$aryHeader["currentTime"] 					= $currentTime;

	$aryHeader["input_exists"] 					= $input_exists;
	$aryHeader["input_string"] 					= $input_string;
	$aryHeader["input_string_file"] 			= $input_string_file;
	$aryHeader["input_string_dir"] 				= $input_string_dir;
	$aryHeader["dirname"] 						= $dirname;
	$aryHeader["basename"] 						= $basename;
	$aryHeader["extension"] 					= $extension;
	$aryHeader["filename"] 						= $filename;
	$aryHeader["file_bytes"] 					= $file_bytes;
	$aryHeader["file_bytes_text"]				= $file_bytes_text;

	$aryHeader["response_sec"] 					= $response_time;
	$aryHeader["response_sec_raw"] 				= $response_time_raw;

	// Create array and prepare for json encoding
	$returnJson["header"] 						= $aryHeader;

//======================================================================================
// Paint it black
//======================================================================================
    echo(json_encode($returnJson));

//======================================================================================
// Paint it black
//======================================================================================
function fileSizeConvert($bytes)
{

	$result = '';

    $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

    foreach($arBytes as $arItem)
    {
        if($bytes >= $arItem["VALUE"])
        {
            $result = $bytes / $arItem["VALUE"];
            $result = str_replace(".", "." , strval(round($result, 2)))." ".$arItem["UNIT"];
            break;
        }
    }

    return $result;
}

?>